﻿///***************************************************************
/// 시  스  템 : Updms.Bby
/// 단위시스템 : BBY
/// 계      층 : Presentation Layer
/// 프로그램명 : Bby2011r
/// 파  일  명 : Bby2011r.aspx.cs
/// 설      명 : 주재료챠트
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-08-28 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Web.UI;

using SMFramework.Data;
using SMFramework.Utility;

using Base.Dac;
using Updms.Bby.Exe;


namespace Updms.Bby
{
	public partial class Bby2011r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Bby2011r";
		public static readonly string gs_path_info = "/updms/Bby/" + PID + ".aspx";

		Global global = new Global();
		CommCode.Com com = new CommCode.Com(LANG_SET);
		CommCode.Bby bby = new CommCode.Bby(LANG_SET);

		//검색
		protected string kw1 = "";	//수유일 ~부터
		protected string kw2 = "";	//수유일 ~까지
		protected string kw3 = "";	//챠트넓이

		//메뉴 선택값
		protected string gs_sm = "";

		// 페이지모드
		private string gs_page_mode = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetClientScript();
			SetBabyFoodStuff();
			SetChartWidth();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetInit, SetClientScript, SetSearchParameter

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		private void SetClientScript()
		{
			//엔터검색
			txt_search_lac_dt_from.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_search_lac_dt_to.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						kw1 = String.IsNullOrEmpty(txt_search_lac_dt_from.Text) ? DateUtil.GetMonthEx(-6) : txt_search_lac_dt_from.Text.Trim();
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						txt_search_lac_dt_from.Text = kw1;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						kw2 = String.IsNullOrEmpty(txt_search_lac_dt_to.Text) ? DateUtil.GetMonthEx(0) : txt_search_lac_dt_to.Text.Trim();
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						txt_search_lac_dt_to.Text = kw2;
					}
					
					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))	// url에 값이 없으면
					{
						kw3 = ddl_search_chart_width.SelectedValue;
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						ddl_search_chart_width.SelectedValue = kw3;
					}
					break;
				case "SEARCH":
					kw1 = String.IsNullOrEmpty(txt_search_lac_dt_from.Text) ? DateUtil.GetMonthEx(-6) : txt_search_lac_dt_from.Text.Trim();
					kw2 = String.IsNullOrEmpty(txt_search_lac_dt_to.Text) ? DateUtil.GetMonthEx(0) : txt_search_lac_dt_to.Text.Trim();
					kw3 = ddl_search_chart_width.SelectedValue;
					break;
			}
		}

		#endregion

		#region 마스터 Method (SetBabyFoodStuff, SetChartWidth)

		//주재료
		private void SetBabyFoodStuff()
		{
			bby.SetBabyFoodStuff(cbl_search_baby_food_stuff);
			WebControlUtil.SelectCheckBoxList(cbl_search_baby_food_stuff, true);
		}

		//챠트넓이
		private void SetChartWidth()
		{
			com.SetChartWidth(ddl_search_chart_width);
			ddl_search_chart_width.SelectedValue = CHART_RESOLUTION;
		}
		
		#endregion

		#region 챠트 (DrawCountWeeklyBabyFoodStuff, SearchProcess)

		//조회
		private void DrawCountWeeklyBabyFoodStuff()
		{
			int li_line_cnt = 0; // 그래프 라인수		
			bool lb_checked = true;

			//초기화
			chart_weekly.Data.Clear();

			//시리즈 수
			chart_weekly.Data.Series = WebControlUtil.GetCheckedItemsCount(cbl_search_baby_food_stuff);

			//넓이
			chart_weekly.Width = Convert.ToInt32(kw3);

			for (int i = 0; i < cbl_search_baby_food_stuff.Items.Count; i++)
			{
				if (cbl_search_baby_food_stuff.Items[i].Selected == true)
				{
					lb_checked = false;

					using (Bby2011r_NT _Bby2011r_NT = new Bby2011r_NT())
					{
						DataSet lo_ds = new DataSet();
						DataPack lo_dp = new DataPack();

						lo_dp.AddProperty("ls_lac_dt_from", typeof(string), kw1);
						lo_dp.AddProperty("ls_lac_dt_to", typeof(string), kw2);
						lo_dp.AddProperty("ls_baby_food_stuff", typeof(string), cbl_search_baby_food_stuff.Items[i].Value);

						lo_ds.Merge(_Bby2011r_NT.GetCountWeeklyBabyFoodStuff(lo_dp));

						int li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_BBY_REPORT");

						for (int j = 0; j < li_count; j++)
						{
							//수동으로 데이터 바인딩
							chart_weekly.Data[li_line_cnt, j] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_BBY_REPORT", j, "Cnt");

							//X축 라벨링
							chart_weekly.AxisX.Labels[j] = DataSetUtil.GetFldStr(lo_ds, "UPDMS_BBY_REPORT", j, "Week_Nm");
						}
						//시리즈
						chart_weekly.Series[li_line_cnt].Text = cbl_search_baby_food_stuff.Items[i].Text;
						li_line_cnt = li_line_cnt + 1;
					}//using
				}//if
			}//for

			// 선택한 주재료가 없으면
			if (lb_checked)
			{
				gs_page_mode = "";
			}
		}

		//검색
		private void SearchProcess()
		{
			gs_page_mode = "R";
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			DrawCountWeeklyBabyFoodStuff();
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}

		// ChangePageMode
		private void ChangePageMode()
		{
			switch (gs_page_mode)
			{
				case "":	// 처음 페이지로드시
					table_chart.Visible = false;
					break;
				case "R":	// 검색버튼 클릭시
					table_chart.Visible = true;
					break;
			}
		}

		#endregion

		#region 버튼이벤트

		//챠트
		protected void ibtn_chart_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}
		
		#endregion

	}
}
